<?php
/**                                          
 * 微博附件
 * @author M2
 * @version 1.0
 * create Date 2013-04-27
 */
class WeiboAttachModel extends Model{
    var $tableName = 'weibo_attach';
	/**
	 * 添加微博关联附件	
	 * @param int $uid 用户ID
	 * @param int $weibo_id 微博ID
	 * @param int $weibo_type 微博类型
	 * @param array $attach_ids
	 */
    public function add($uid, $weibo_id, $weibo_type, $attach_ids){
		$uid	=	intval($uid);
		$weibo_id = intval($weibo_id);
		$weibo_type = intval($weibo_type);
		if(!$uid || !$weibo_id || !$weibo_type || !$attach_ids)
			return false;
		//解析attach_ids
		$attach_ids	=	$this->_parseAttachIds($attach_ids);
		//格式化数据
		if( is_array($attach_ids) && count($attach_ids)>0 ){
			$insert_sql	=	"INSERT INTO __TABLE__ (uid,weibo_id,weibo_type,attach_id) VALUES ";
			foreach($attach_ids as $attach_id){
				$insert_sql	.=	" ($uid,$weibo_id,$weibo_type,$attach_id) ,";
			}
			$insert_sql	=	rtrim($insert_sql,',');
			//插入数据列表
			$result	=	$this->execute($insert_sql);
			return $result;
		}else{
			return false;
		}
	}

	/**
	 * 解析参数
	 * @param array|Object|string $input
	 * @return array
	 */
	 private function _parseAttachIds($input){
		if(is_string($input)){
			$input = explode(',',$input);
		}
		$output	=	array();
		if(is_array($input) || is_object($input)){
			foreach($input as $v){
				if(intval($v) > 0)
					$output[]	=	intval($v);
			}
		}
		return $output;
	}
}